package bibliotecas;
import java.awt.Color;

import javax.swing.*;
import javax.swing.table.*;

public class JSmartTable extends JPanel{

	private static final long serialVersionUID = 3395638940093312597L;
	
	public DefaultTableModel modeloTabela = null;
	public JTable tabela = null;
	public JScrollPane scrollTabela = null;
	
	private void inicializacao(String colunas[],int larguras[]){
		this.setLayout(null);
		this.setBackground(new Color(255,255,255));
		
		modeloTabela = new DefaultTableModel(colunas,0);
		tabela = new JTable(modeloTabela){
			private static final long serialVersionUID = -3478144567380102937L;
			@Override
			public boolean isCellEditable(int x,int y){
				return false;
			}
		};
		tabela.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollTabela = new JScrollPane(tabela);
		if(larguras != null)
		for(int i = 0;i < tabela.getColumnCount();i++){
			tabela.getColumnModel().getColumn(i).setPreferredWidth(larguras[i]);
		}
	}
	
	public void setLarguras(int larguras[]){
		if(larguras != null)
		for(int i = 0;i < tabela.getColumnCount();i++){
			tabela.getColumnModel().getColumn(i).setPreferredWidth(larguras[i]);
		}
	}
	
	public JSmartTable(String colunas[]){		
		inicializacao(colunas,null);
	}
	
	public JSmartTable(String colunas[],int larguras[]){
		inicializacao(colunas,larguras);
	}
	
	public void setDimensoes(int x,int y,int width,int height){
		setBounds(x,y,width,height);
		scrollTabela.setBounds(0,0,width,height);
		add(scrollTabela);
	}
	
	public void setConteudo(String dados[][]){
		modeloTabela.setRowCount(0);
		for(int i = 0;i < dados.length ;i++){
			modeloTabela.addRow(dados[i]);
		}
	}
	
	public void setAlinharColuna(int coluna,int tipo){
		DefaultTableCellRenderer alinhar = new DefaultTableCellRenderer();
		  
		alinhar.setHorizontalAlignment(tipo);
		  
		tabela.getColumnModel().getColumn(coluna).setCellRenderer(alinhar);
	}
	
	public String[] getConteudoSelecionado(){
		String dados[] = new String[tabela.getColumnCount()];
		for(int i = 0;i < dados.length;i++){
			dados[i] = tabela.getValueAt(tabela.getSelectedRow(),i).toString();
		}
		return dados;
	}
	
}
